/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.purpose.controller;

import com.koron.common.StaffAttribute;
import com.koron.common.bean.StaffBean;
import com.koron.common.domain.DataBean;
import com.koron.common.permission.SPIAccountAnno;
import com.koron.purpose.domain.ContractTemplate;
import com.koron.purpose.service.api.ContractTemplateService;
import com.koron.purpose.utils.BusinessCode;
import com.koron.system.annotation.Log;
import com.koron.system.discrete.BusinessManipulation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;
import java.util.Date;

@Api(tags = "contracttemplate", description = "责任书模板接口")
@RestController
@RequestMapping("/contracttemplate")
public class ContractTemplateController {

    @Autowired
    private ContractTemplateService templateService;


    private void setProperties(ContractTemplate source, StaffBean user) {
        source.setAddUser(user.getCode());
        source.setAddUserName(user.getName());
        source.setAddTime(new Date());
        source.setUpdateUser(user.getCode());
        source.setUpdateUserName(user.getName());
        source.setUpdateTime(source.getAddTime());
    }


    @ApiOperation("存储责任书模板")
    @RequestMapping(value = "/save.htm", method = {RequestMethod.POST})
    @Log(title = "新增、修改责任书模板", businessManipulation = BusinessManipulation.INSERT)
    public MessageBean<Boolean> save(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @RequestBody ContractTemplate contract) {
        if (user == null) {
            return MessageBean.create(BusinessCode.NO_LOGIN.getCode(), BusinessCode.NO_LOGIN.getDescription(), Boolean.class);
        }
        if (contract == null) {
            return MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), Boolean.class);
        }
        setProperties(contract, user);
        Integer result = templateService.persist(contract);
        BusinessCode bc = BusinessCode.valueOf(result);
        return MessageBean.create(bc.getCode(), bc.getDescription(), Boolean.class);
    }


    @ApiOperation("批量删除责任书模板")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "考核指标Id,多个之间用逗号分开", required = true, dataType = "String", paramType = "query")
    })
    @RequestMapping(value = "/batchDelete.htm", method = RequestMethod.POST)
    @Log(title = "批量删除责任书模板", businessManipulation = BusinessManipulation.DELETE)
    public MessageBean<Boolean> batchDelete(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String ids) {
        MessageBean<Boolean> msg = new MessageBean<>();
        if (null == user) {
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        if (StringUtils.isEmpty(ids)) {
            return MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), Boolean.class);
        }
        try {
            String[] idArray = ids.split("[,，]");
            msg.setData(templateService.batchDetele(Arrays.asList(idArray)));
            msg.setCode(BusinessCode.SUCCESS.getCode());
            msg.setDescription(BusinessCode.SUCCESS.getDescription());
            return msg;
        } catch (Exception e) {
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
            return msg;
        }
    }


    @ApiOperation("查询列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "year", value = "年份",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "考核指标名",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "当前页", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, dataType = "String", paramType = "query")
    })
    @RequestMapping(value = "/list.htm", method = RequestMethod.POST)
    public MessageBean<DataBean<ContractTemplate>> list(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @RequestParam(required = false) String year, @RequestParam(required = false) String name,
                                                        @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "15") Integer pageSize) {
        MessageBean<DataBean<ContractTemplate>> msg = new MessageBean<>();
        if (null == user) {
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        Integer start = (page <= 1) ? 0 : (page - 1) * pageSize;
        try {
            DataBean<ContractTemplate> dataBean = templateService.fetchContractTemplates(year, name, start, pageSize);
            msg.setData(dataBean);
            msg.setCode(BusinessCode.SUCCESS.getCode());
            msg.setDescription(BusinessCode.SUCCESS.getDescription());
        } catch (Exception e) {
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
        }
        return msg;
    }


    @ApiOperation("责任书模板详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "考核指标Id", required = true, dataType = "String", paramType = "query")
    })
    @RequestMapping(value = "/detail.htm", method = RequestMethod.POST)
    public MessageBean<ContractTemplate> detail(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @RequestParam(required = true) final String id) {
        MessageBean<ContractTemplate> msg = new MessageBean<>();
        if (null == user) {
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        if (StringUtils.isEmpty(id)) {
            msg.setCode(BusinessCode.INSUFFICIENT_PARAM.getCode());
            msg.setDescription(BusinessCode.INSUFFICIENT_PARAM.getDescription());
            return msg;
        }
        try {
            ContractTemplate kpiTemplate = templateService.fetchOne(id);
            msg.setCode(BusinessCode.SUCCESS.getCode());
            msg.setDescription(BusinessCode.SUCCESS.getDescription());
            msg.setData(kpiTemplate);
        } catch (Exception e) {
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
        }
        return msg;
    }


}
